﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace Directory.Web
{
	public partial class Admin_User : System.Web.UI.Page
	{
		protected string usernamekey = "username";
		protected string username;
		protected void Page_Load(object sender, EventArgs e)
		{
			string action = null;
			action = Request.QueryString["action"];
			username = Server.UrlDecode(Request.QueryString[usernamekey] == null ? "" : Request.QueryString[usernamekey]);
			if (!IsPostBack)
			{
				this.btnDelete.Attributes.Add("onclick", "return(window.confirm('确认删除该用户吗？'))");
				switch (action)
				{
					case "delete":
						DeleteUser();
						break;
					case "modify":
						RenderModify();
						break;
					default:
						return;
				}
			}
		}
		// 删除用户
		private void DeleteUser()
		{
			try
			{
				UserDataProxy.DeleteUser(username);
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('用户删除成功。');parent.RefreshPage();</script>");
			}
			catch
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('服务器处理错误：未知错误。');</script>");
			}
		}
		// 绑定用户信息
		private void RenderModify()
		{
			Directory.Model.UserInfo userInfo = null;
			try
			{
				userInfo = UserDataProxy.GetUserInfo(username);
			}
			catch (ArgumentException)
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('用户名格式不正确');parent.ResetPage();</script>");
				return;
			}

			if (userInfo == null)
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('用户不存在。');parent.ResetPage();</script>");
				return;
			}

			ltName.Text = userInfo.UserName;
			ltEmail.Text = userInfo.Email;
			ltNickName.Text = userInfo.NickName;
			ltRealName.Text = userInfo.RealName;
			ltDescription.Text = userInfo.Description;
			ltPIN.Text = userInfo.PIN;
			ltQQ.Text = userInfo.QQ;
			ltHandSet.Text = userInfo.HandSet;
			ltPhone.Text = userInfo.Phone;
			ltMSN.Text = userInfo.MSN;
			ltAddress.Text = userInfo.Address;
			ltPostCode.Text = userInfo.PostCode;
			ltWebpage.Text = userInfo.Webpage;
			ltCreateDate.Text = userInfo.CreateDate.ToString();
			ltLastLoginDate.Text = userInfo.LastLoginDate.ToString();

			if (userInfo.IsLockedOut)
			{
				this.btnLock.Enabled = false;
				this.btnLock.Visible = false;
			}
			else
			{
				this.btnUnlock.Enabled = false;
				this.btnUnlock.Visible = false;
			}

			#region binding roles

			int roleRecords;
			string[] allRoles = RoleDataProxy.GetAllRoles(0, 999, out roleRecords);
			string[] userRoles = RoleDataProxy.GetRolesForUser(username, 0, 999, out roleRecords);
			foreach (string role in allRoles)
			{
				ListItem item = new ListItem(role, role);
				item.Selected = IsArraryContains(userRoles, role);
				this.cblList.Items.Add(item);
			}

			#endregion
		}
		// 锁定用户
		protected void btnLock_Click(object sender, EventArgs e)
		{
			string username = Request.QueryString[usernamekey];
			try
			{
				UserDataProxy.LockUser(username);
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('锁定成功。');parent.RefreshPage();</script>");
			}
			catch
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('服务器处理错误：未知错误。');</script>");
			}
		}
		// 解锁用户
		protected void btnUnlock_Click(object sender, EventArgs e)
		{
			string username = Request.QueryString[usernamekey];
			try
			{
				UserDataProxy.UnLockUser(username);
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('解锁成功。');parent.RefreshPage();</script>");
			}
			catch
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('服务器处理错误：未知错误。');</script>");
			}
		}
		// 删除用户
		protected void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
				UserDataProxy.DeleteUser(username);
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('用户删除成功。');parent.RefreshPage();</script>");
			}
			catch
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('服务器处理错误：未知错误。');</script>");
			}
		}
		// 保存用户角色
		protected void btnSaveRole_Click(object sender, EventArgs e)
		{
			try
			{
				int roleRecords;
				string[] userRole = RoleDataProxy.GetRolesForUser(username,0,999,out roleRecords);
				foreach (ListItem item in this.cblList.Items)
				{
					if (item.Selected && !IsArraryContains(userRole, item.Value))
					{
						RoleDataProxy.AddUserToRole(username, item.Value);
					}
					else if (!item.Selected && IsArraryContains(userRole, item.Value))
					{
						RoleDataProxy.RemoveUserFromRole(username, item.Value);
					}
				}
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('保存成功。');window.location.replace(window.location.href);</script>");
			}
			catch
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('服务器处理错误：未知错误。');window.location.replace(window.location.href);</script>");
			}
		}

		private bool IsArraryContains(string[] array, string item)
		{
			foreach (string str in array)
			{
				if (item == str)
				{
					return true;
				}
			}
			return false;
		}
}
}
